package com.lyj.hw_student.system.controller;


import com.lyj.hw_student.system.entity.SysUser;
import com.lyj.hw_student.system.service.ISysUserService;
import com.lyj.hw_student.system.service.impl.SysUserServiceImpl;
import io.swagger.annotations.Api;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lyj
 * @since 2025-04-02
 */
@RestController
@RequestMapping("/sysUser")
public class SysUserController {
   @Autowired
   ISysUserService sysUserService;
   @PostMapping("/save")
   public Map<String,Object> save(@RequestBody SysUser sysUser) {
       Map<String,Object> map = new HashMap<>();
       sysUserService.save(sysUser);
       map.put("success","success");
        return map;
   }
    @GetMapping("/login")
    public String login(@RequestParam("userName") String userName, @RequestParam("password") String password) {
        try {
            SysUser user = sysUserService.findByName(userName);
            if (user == null) {
                return "用户名不存在";
            }
            if (!user.getPassword().equals(password)) {
                return "密码错误";
            }
            return "success";
        } catch (Exception e) {
            return "服务器内部错误: " + e.getMessage();
        }
    }
    @RequestMapping("/test")
    public String test(){
        return "test";
    }

    /**
     * 1新增一个角色
     * param sysUser sysUser
     * @return String
     */

    @PostMapping("/adduser")
    public String adduser(@RequestBody SysUser sysUser){
        sysUserService.save(sysUser);
        return "adduser";
    }

    /**
     * 2删除用户
     * @param userId Integer
     * @return String
     */
    @DeleteMapping("/delUser/{userId}")
    public String delUser(@PathVariable Integer userId){
        sysUserService.removeById(userId);
        return "ok";
    }

    /**
     *3 修改一个角色
     * @param sysUser SysUser
     * @return String
     */
    @PutMapping("/updateUser")
    public String updateUser(@RequestBody SysUser sysUser) {
         sysUserService.updateById(sysUser);
        return "Success";
    }

    /**
     * 4查找一个角色
     * @param userId Integer
     * @return sysUser
     */
    @GetMapping("/selectUser/{userId}")
    public SysUser selectUser(@PathVariable Integer userId) {
       SysUser sysUser = sysUserService.getById(userId);
        return sysUser;
    }










}
